function confirmSubmit(msg){
	var agree = confirm(msg);
	if (agree){
		return true;
	}else{
		return false;
	}
}
/****************************************************************************************************/

function getFormData(formId){
	formElements = document.getElementById(formId).elements;
	var result = '';

	for (var i=0; i < formElements.length; i++){
		if(formElements[i].type=='text' || formElements[i].type=='password' || formElements[i].type=='hidden' || formElements[i].type=='textarea'){
			result += formElements[i].name + '=' + encodeURI(formElements[i].value) + '&';
		}else if(formElements[i].type=='select-one' || formElements[i].type=='select-multiple'){
			for (var j = 0; j < formElements[i].options.length; j++){
				if(formElements[i].options[j].selected){
					result += formElements[i].name + '=' + encodeURI(formElements[i].value) + '&';
				}
			}
		}else if((formElements[i].type=='checkbox' || formElements[i].type=='radio') && formElements[i].checked){
			result += formElements[i].name + '=' + encodeURI(formElements[i].value) + '&';
		}
	}

	return result;
}
/****************************************************************************************************/

function appendResult (result, append){
	if(result == ''){
		return append;
	}

	var app = append.split('::');
	var res = result.split('|');
	var ret = '';
	var flag = false;

	for (var i = 0; i < res.length; i++){
		if(res[i].indexOf('|' + app[0] + '::') != -1 || res[i].substring(0,(app[0] + '::').length) == (app[0] + '::')){
			flag = true;
			res[i] = append;
		}
		ret = ret + '|' + res[i];
	}

	if(!flag){
		ret = ret + '|' + append;
	}

	//alert(result + '<>' + append + '<>' + ret.substring(1,ret.length));
	return ret.substring(1,ret.length);
}
/****************************************************************************************************/

function cleanFormElements(formElements, params){
	if(params == ''){
		return params;
	}

	var index = -1;
	var result = '|' + params + '|';
	var prepend = '';
	var append = '';
	for (var j=0; j < formElements.length; j++){
		index = result.indexOf('|' + formElements[j].name + '::');
		if(index != -1){
			prepend = result.substring(0, index);
			append = result.substring(index + 1);
			//alert('before -> ' + result);
			result = prepend + append.substring(append.indexOf('|'));
			//alert('after -> ' + result);
		}
	}

	result = result.substring(1);
	if(result.length > 0){
		result = result.substring(0, result.length - 1);
	}

	return result;
}

/****************************************************************************************************/

function fetchForm (formId, paramsId){
	formElements = document.getElementById(formId).elements;
	var append = '';
	var result = '';
	var val = '';
	if(document.getElementById(paramsId) && document.getElementById(paramsId).value != ''){
		result = cleanFormElements(formElements, document.getElementById(paramsId).value);
	}

	for (var i=0; i < formElements.length; i++){
		if(formElements[i].id != paramsId){
			if(formElements[i].type=='text' || formElements[i].type=='password' || formElements[i].type=='hidden' || formElements[i].type=='textarea'){
				val = php_serialize(formElements[i].value);
				append = formElements[i].name + '::' + val;
				result = appendResult(result, append);
			}else if(formElements[i].type=='select-one' || formElements[i].type=='select-multiple'){
				for (var j = 0; j < formElements[i].options.length; j++){
					if(formElements[i].options[j].selected){
						val = php_serialize(formElements[i].options[j].value);
						append = formElements[i].name + '::' + val;
						result = appendResult(result, append);
					}
				}
			}else if((formElements[i].type=='checkbox' || formElements[i].type=='radio') && formElements[i].checked){
				val = php_serialize(formElements[i].value);
				append = formElements[i].name + '::' + val;
				result = appendResult(result, append);
			}
		}
	}

	return result;
}
/****************************************************************************************************/

function getChildList (parentId){
	var obj = document.getElementById(parentId);
	if(!obj){
		return false;
	}
	var count = obj.childNodes.length;
	var res = new Array();
	var j=0;
	for (var i=0; i<count; i++){
		if(obj.childNodes[i].nodeName != '#text'){
			res[j] = obj.childNodes[i];
			j++;
		}
	}
	return res;
}
/****************************************************************************************************/

function ShowHide(action, style, id){
	var set_style = '';
	var status = '';
	var element = document.getElementById(id);
	
	if(!element){
		return false;
	}
	
	if(style != 'visibility' && style != 'display'){
		return false;
	}
	if(action != 'show' && action != 'hide'){
		return false;
	}

	if(style == 'visibility'){
		if(action == 'show'){
			element.style.visibility = 'visible';
		}else if(action == 'hide'){
			element.style.visibility = 'hidden';
		}
	}else if(style == 'display'){
		if(action == 'show'){
			element.style.display = 'block';
		}else if(action == 'hide'){
			element.style.display = 'none';
		}
	}
		
}
/****************************************************************************************************/

function php_serialize(obj) {
	var string = '';

	if (typeof(obj) == 'object') {

		if (obj instanceof Array) {
			string = 'a:';
			tmpstring = '';
			count = 0;
			for (var key in obj) {
				tmpstring += php_serialize(key);
				tmpstring += php_serialize(obj[key]);
				count++;
			}
			string += count + ':{';
			string += tmpstring;
			string += '}';
		} else if (obj instanceof Object) {
			classname = obj.toString();

			if (classname == '[object Object]') {
				classname = 'StdClass';
			}

			string = 'O:' + classname.length + ':"' + classname + '":';
			tmpstring = '';
			count = 0;
			for (var key in obj) {
				tmpstring += php_serialize(key);
				if (obj[key]) {
					tmpstring += php_serialize(obj[key]);
				} else {
					tmpstring += php_serialize('');
				}
				count++;
			}
			string += count + ':{' + tmpstring + '}';
		}
		alert('as');
	} else {
		switch (typeof(obj)) {
			case 'number':
				if (obj - Math.floor(obj) != 0) {
					string += 'd:' + obj + ';';
				} else {
					string += 'i:' + obj + ';';
				}
			break;
			case 'string':
				string += 's:' + obj.length + ':"' + obj + '";';
			break;
			case 'boolean':
				if (obj) {
					string += 'b:1;';
				} else {
					string += 'b:0;';
				}
			break;
		}
	}

	return string;
}
/****************************************************************************************************/

function show_clientlocation(op) {
	if(op == 'nolocationselected') {
		document.getElementById('nolocationselected_clientlocationSpan').style.display = 'block';
		document.getElementById('euclient_clientlocationSpan').style.display = 'none';
		document.getElementById('noneuclient_clientlocationSpan').style.display = 'none';
	}else if(op == 'euclient') {
		document.getElementById('nolocationselected_clientlocationSpan').style.display = 'none';
		document.getElementById('euclient_clientlocationSpan').style.display = 'block';
		document.getElementById('noneuclient_clientlocationSpan').style.display = 'none';
	}else if(op == 'noneuclient') {
		document.getElementById('nolocationselected_clientlocationSpan').style.display = 'none';
		document.getElementById('euclient_clientlocationSpan').style.display = 'none';
		document.getElementById('noneuclient_clientlocationSpan').style.display = 'block';
	}
}
/****************************************************************************************************/

function show_period(op) {
	if(op == 'noperiodselected') {
		document.getElementById('noperiodselected_periodSpan').style.display = 'block';
		document.getElementById('monthyear_periodSpan').style.display = 'none';
		document.getElementById('customperiod_periodSpan').style.display = 'none';
	}else if(op == 'monthyear') {
		document.getElementById('noperiodselected_periodSpan').style.display = 'none';
		document.getElementById('monthyear_periodSpan').style.display = 'block';
		document.getElementById('customperiod_periodSpan').style.display = 'none';
	}else if(op == 'customperiod') {
		document.getElementById('noperiodselected_periodSpan').style.display = 'none';
		document.getElementById('monthyear_periodSpan').style.display = 'none';
		document.getElementById('customperiod_periodSpan').style.display = 'block';
	}
}
/****************************************************************************************************/

function changeTab(element_id){
	var selection = getChildList('menu_selection');
	//alert(selection.length);
	for (var i = 0; i < selection.length; i++){
		//alert(selection[i]);
		if(selection[i].id){
			if(selection[i].id == element_id){
				selection[i].className = 'selected';
			}else{
				selection[i].className = 'notselected';
			}
		}
	}
}
/****************************************************************************************************/

function loadinga(){
	ShowHide('show', 'visibility', 'load');
}
/****************************************************************************************************/

Array.prototype.inArray = function (value){
        var i;
        for (i=0; i < this.length; i++) {
                if (this[i] === value) {
                        return true;
                }
        }
        return false;
}
/****************************************************************************************************/

function page(val){
	var offset = document.getElementById('offset');
	var limit = document.getElementById('limit');
	var formId = document.getElementById('searchFormId').value;
	var form = document.getElementById(formId);
	var current_page = offset.value / limit.value + 1;

        if(val == 'prev' && valid_page_num(current_page - 1)){
        	page(current_page - 1);
        }
        
        if(val == 'next' && valid_page_num(current_page + 1)){
		page(current_page + 1);
        }
        
        if(valid_page_num(val)){
        	offset.value = limit.value * (val - 1);
        	//document.getElementById('submit_button').click();
        }
}
/****************************************************************************************************/

function valid_page_num(page_num){
        var pages = document.getElementById('num_pages').value;
        var ValidChars = "0123456789";
        var IsNumber = true;
        var Char;

        for (i = 0; i < page_num.length && IsNumber == true; i++) {
                Char = page_num.charAt(i);
                if (ValidChars.indexOf(Char) == -1) {
                        IsNumber = false;
			break;
                }
        }

	//alert(page_num + ' <= ' + pages);
//	alert(page_num <= pages);

//	alert(isInteget);
        if(IsNumber && parseInt(page_num) >= 1 && parseInt(page_num) <= parseInt(pages)){
                return true;
        }else{
                return false;
        }
}
/****************************************************************************************************/

function openPage(page, params){
	//alert(params);
	loadinga();
	var no_tab_change = new Array('account(billing).html', 'account(domains).html');
	if(!no_tab_change.inArray(page)){
		changeTab(page);
	}
	xajax_openPage(page, params);
}
/****************************************************************************************************/

function sgeneral(params){
	loadinga();
	xajax_Run('sgeneral', params);
}
/****************************************************************************************************/

function searchOrders(params){
	loadinga();
	xajax_Run('searchOrders', params);
}
/****************************************************************************************************/

function loginClient(params){
	xajax_Run('loginClient', params);
}
/****************************************************************************************************/

function toggle(type, element_id, container_id){
	var childrenList = getChildList(container_id);
	if(!childrenList){
		return false;
	}
	var count = childrenList.length;
	
	filter_table('set', '');
	for(var i = 0; i < count; i++){
		if(element_id == childrenList[i].id){
			ShowHide('show', type, childrenList[i].id);
		}else{
			//alert(filter(childrenList[i].id));
			filter_table('append', filter(childrenList[i].id));
			ShowHide('hide', type, childrenList[i].id);
		}
	}
}
/****************************************************************************************************/

function getElementHeight(Elem) {
	var elem = document.getElementById(Elem);
	xPos = elem.offsetHeight;
	return xPos;
}
/****************************************************************************************************/

function getElementWidth(Elem) {
	var elem = document.getElementById(Elem);
	xPos = elem.offsetWidth;
	return xPos;
}
/****************************************************************************************************/

function clear_all(element_id, container_id){
	var childrenList = getChildList(container_id);
	var formId = document.getElementById('searchFormId').value;
	var form = document.getElementById(formId);
        var count = childrenList.length;

        for(var i = 0; i < count; i++){
                if(element_id != childrenList[i].id){
			form.elements['info[' + childrenList[i].id + ']'].value = '';
                }
        }
}
/****************************************************************************************************/

function setSearchValue(key, value){
        var formId = document.getElementById('searchFormId').value;
        var form = document.getElementById(formId);
	if(value != ''){
		form.elements['info[' + key + ']'].value = value;
	}
}
/****************************************************************************************************/

function unsetLimitOffset(){
	if(document.getElementById('limit')){
		document.getElementById('limit').value = '';
	}
	if(document.getElementById('offset')){
		document.getElementById('offset').value = '';
	}
}
/****************************************************************************************************/

function filter(val){
	var obj = document.getElementById('info_' + val);
	var option = document.getElementById('selected_' + val);
	var optionText = val;
	if(obj && obj.value != ''){
		if(option){
			optionText = option.text;
		}
		return '<div><div style="float:left;width:150px;">' + optionText + '</div><div style="width:230px;float:left;">' + obj.value + '</div><div style="width:14px;float:left;"><a href="#" style="color:red;" onClick="document.getElementById(\'info_'+ val +'\').value = \'\';toggle(\'display\', document.getElementById(\'select_params\').value, \'values\');return false;">[x]</a></div><div style="clear:both;"></div></div>';
	}
	return '';
}
/****************************************************************************************************/

function filter_table(action, htmlSource){
	if(action == 'set'){
		document.getElementById('filter').innerHTML = htmlSource;
	}else if(action == 'append'){
		document.getElementById('filter').innerHTML = document.getElementById('filter').innerHTML + htmlSource;
	}else if(action == 'prepend'){
		document.getElementById('filter').innerHTML = htmlSource + document.getElementById('filter').innerHTML;
	}
}
/****************************************************************************************************/

function show_filter(){
	if(document.getElementById('filter') && document.getElementById('filter').innerHTML != ''){
		ShowHide('show', 'display', 'filter');
	}
}
/****************************************************************************************************/

function hide_filter(){
	ShowHide('hide', 'display', 'filter');
}